package com.ruifu.bom.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.bom.dto.MeteringUnitsAddDTO;
import com.ruifu.bom.dto.MeteringUnitsDTO;
import com.ruifu.bom.dto.MeteringUnitsFindListDTO;
import com.ruifu.bom.dto.MultipleDTO;
import com.ruifu.bom.mapper.MeteringUnitsMapper;
import com.ruifu.bom.service.MeteringUnitsService;
import com.ruifu.bom.vo.MeteringUnitsVO;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.MeteringUnits;
import com.ruifu.common.utils.BeanConverter;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 计量单位控制层
 *
 * @author nyr
 * @create 2019/12/24
 */
@RestController
@CrossOrigin
@RequestMapping("/meteringUnits")
public class MeteringUnitsController {
    @Autowired
    private MeteringUnitsService meteringUnitsService;
    @Autowired(required = false)
    private MeteringUnitsMapper meteringUnitsMapper;

    /**
     * 查询计量单位列表
     *
     * @param meteringUnitsFindListDTO
     * @return
     */
    @PostMapping("/findList")
    public R findList(@RequestBody MeteringUnitsFindListDTO meteringUnitsFindListDTO) {
        if (StringUtils.isNotEmpty(meteringUnitsFindListDTO.getMuChineseName())) {
            return R.ok(BeanConverter.copyList(meteringUnitsService.list(Wrappers.<MeteringUnits>lambdaQuery()
                    .eq(MeteringUnits::getSetSid, SecurityUtils.getUser().getTenantId())
                    .like(MeteringUnits::getMuChineseName, meteringUnitsFindListDTO.getMuChineseName())
                    .orderByAsc(MeteringUnits::getMuBasicUnitId)
                    .orderByAsc(MeteringUnits::getMuBasicUnitMultiple)
            ), MeteringUnitsVO.class));
        } else {
            return R.ok(BeanConverter.copyList(meteringUnitsService.list(Wrappers.<MeteringUnits>lambdaQuery()
                    .eq(MeteringUnits::getSetSid, SecurityUtils.getUser().getTenantId())
                    .orderByAsc(MeteringUnits::getMuBasicUnitId)
                    .orderByAsc(MeteringUnits::getMuBasicUnitMultiple)
            ), MeteringUnitsVO.class));
        }

    }

    /**
     * 根据id查询单位
     * @param idList
     * @return
     */
    @PostMapping("/findByIds")
    public R findList(@RequestBody List<Long> idList) {
        List<MeteringUnits> meteringUnitsList=new ArrayList<>();
        if (idList!=null && idList.size()!=0){
            for (Long id : idList) {
                meteringUnitsList.add(meteringUnitsMapper.selectById(id));
            }
        }
        return R.ok(meteringUnitsList);
    }

    /**
     * 新增计量单位
     *
     * @param meteringUnitsAddDTO
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody MeteringUnitsAddDTO meteringUnitsAddDTO) {
        MeteringUnitsDTO meteringUnitsDTO = new MeteringUnitsDTO();
        try {
            List<MultipleDTO> dtoList = meteringUnitsAddDTO.getMultipleDTOList();
            for (MultipleDTO multipleDTO : dtoList) {
                meteringUnitsDTO.setMuBasicUnitId(multipleDTO.getMuBasicUnitId());
                meteringUnitsDTO.setMuBasicUnitMultiple(multipleDTO.getMuBasicUnitMultiple());
                meteringUnitsDTO.setMuChineseName(meteringUnitsAddDTO.getMuChineseName());
                meteringUnitsDTO.setMuEnglishName(meteringUnitsAddDTO.getMuEnglishName());
                meteringUnitsDTO.setMuIsBasicUnit(meteringUnitsAddDTO.getMuIsBasicUnit());
                meteringUnitsService.save(meteringUnitsDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("添加成功");
    }

    /**
     * 修改计量单位
     *
     * @param unitsDTOList
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody List<MeteringUnitsDTO> unitsDTOList) {

        try {
            for (MeteringUnitsDTO meteringUnitsDTO : unitsDTOList) {
                meteringUnitsService.updateById(meteringUnitsDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("修改成功");
    }

    /**
     * 删除计量单位
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody List<Long> ids) {
        try {
            for (Long id : ids) {
                meteringUnitsMapper.deleteById(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("删除成功");
    }
}
